/*********************************************************************************
 *FileName:  sensor.c
 *Author:    ChenJiehong
 *Version:   V1.0
 *Date:      2023-11-29
 ****************************************Includes***********************************/
#include "sensor.h"

/***************************************Variables***********************************/
Position_Para_t Position_Para;

/***************************************Functions***********************************/
/**
 * @brief 位置百分比换算成传感器输出值
 * @param Para: 位置传感器参数; u16PositionPercent: 位置百分比
 * @retval SenserValue: 电阻百分比
 */
uint16_t Position2SenserValue(Position_Para_t *Para, uint16_t u16PositionPercent)
{
    uint16_t SenserValue;

    if (u16PositionPercent > Para->u16PositionPercentage_Max)
    {
        SenserValue = Para->u16Sensor_Max;
    }
    else if (u16PositionPercent < Para->u16PositionPercentage_Min)
    {
        SenserValue = Para->u16Sensor_Min;
    }

    else
    {
        SenserValue = Para->u16Sensor_Min + ((u16PositionPercent - Para->u16PositionPercentage_Min) *
                                                 (Para->u16Sensor_Max - Para->u16Sensor_Min) +
                                             (Para->u16PositionPercentage_Max - Para->u16PositionPercentage_Min) / 2) /
                                                (Para->u16PositionPercentage_Max - Para->u16PositionPercentage_Min);
    }

    return SenserValue;
}

/**
 * @brief 传感器输出装换算成位置百分比
 * @param Para: 位置传感器参数; SenserValue: 传感器输出值
 * @retval PositionPercent: 位置百分比
 */
uint16_t SenserValue2Position(Position_Para_t *Para, uint16_t SenserValue)
{
    uint16_t PositionPercent;

    if (SenserValue >= Para->u16Sensor_Max)
    {
        PositionPercent = Para->u16PositionPercentage_Max;
    }
    else if (SenserValue <= Para->u16Sensor_Min)
    {
        PositionPercent = Para->u16PositionPercentage_Min;
    }
    else
    {
        PositionPercent = ((SenserValue - Para->u16Sensor_Min) * (Para->u16PositionPercentage_Max - Para->u16PositionPercentage_Min) +
                           (Para->u16Sensor_Max - Para->u16Sensor_Min) / 2) /
                              (Para->u16Sensor_Max - Para->u16Sensor_Min) +
                          Para->u16PositionPercentage_Min;
    }
    return PositionPercent;
}
/***************************************END OF FILE*********************************/
